﻿using AutoMapper;
using AutoMapper.Internal.Mappers;
using Microsoft.EntityFrameworkCore;
using rbac.Contracts.DTO.Input;
using rbac.Contracts.DTO.Output;
using rbac.Contracts.Interface;
using rbac.Domain.Entity;
using rbac.Domain.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace rbac.Services
{
    public class UserService : ServiceBase,IUserService
    {
        private readonly IUserRepository _userRepository;
        /*private readonly IMapper _objectMapper;*/
        private readonly IUserRoleRepository _useroleRepository;
        private readonly IRoleRepository _roleRepository;


        public UserService(IUserRepository userRepository, IUserRoleRepository userroleRepository, IRoleRepository roleRepository)
        {
            _userRepository = userRepository;
            /*_objectMapper = objectMapper;*/
            _useroleRepository = userroleRepository;
             _roleRepository = roleRepository;
        }

        public PageListOutput<UserOutput> GetUsers(GetUsersInput input)
        {
            var list=_userRepository.GetUsers().Where(c=>c.IsDeleted==false).AsNoTracking();
            if(!string.IsNullOrEmpty(input.UserName))
            {
                //如果我们在UserName上面建立了索引， 那最好使用StartWith
                list = list.Where(c => c.UserName!.StartsWith(input.UserName));
            }
            if (!string.IsNullOrEmpty(input.Email))
            {
                
                list = list.Where(c => c.Email!.Contains(input.Email));
            }
           
            //创建分页对象
            PageListOutput<UserOutput> result = new()
            { 
             Total=list.Count(),
             PageSize=input.PageSize
            };
            //分页
            var list1 = list.OrderBy(c => c.Id).Skip(input.PageSize * (input.PageIndex - 1)).Take(input.PageSize).ToList();
            //用户Id集合
            var userIdList = list1.Select(c => c.Id);
            //根据用户Id查询角色
            var roleList = (from ur in _useroleRepository.UserRoles()
                           join r in _roleRepository.Roles()
                           on ur.RoleId equals r.Id
                           where userIdList.Contains(r.Id)
                           select new
                           {
                                ur.UserId,
                                r.ReleName,
                                r.Id
                           }).ToList();
            var roleGroup=roleList.GroupBy(c => c.UserId).ToList();
            result.Data = _mapper.Map<List<UserOutput>>(list1);
   
                foreach (var g in roleGroup)
                {
                var user = result.Data.FirstOrDefault(c => c.Id == g.Key);
                if (user!=null)
                {
                    user.Roles = g.ToList().Select(r => new RoleOutput
                    {
                        Id = r.Id,
                        ReleName = r.ReleName,
                    }).ToList();
                }
            }
            return result;





        }
        public UserOutput Login(LoginInput input)
        {
            var user = _userRepository.Login(input.UserName, input.Pwd);
            var dto= _mapper.Map<UserOutput>(user);
            if(dto==null)
            {
                return dto;
            }
              dto.Roles = _mapper.Map<List<RoleOutput>>(_useroleRepository.UserRolesByUserId(user.Id));
            return dto;
        }
    }
}
